#!/usr/bin/perl

use JSON;

$success = 0;
$jsonObj = {};
foreach $file (@ARGV){
	if(open(LINK, "<$file.link")){
		($problem, $startString, $endString) = <LINK>;
		close(LINK);
		$problem =~ s/\r?\n$//;
		$startString =~ s/\r?\n$//;
		$endString =~ s/\r?\n$//;
	}
	if(open(FILE, "<$file")){
		$nodeName = $file;
		$nodeName =~ s|.*/||;
		#$jsonObj->{$nodeName}->{"Status"} = "maybe running";
		$jsonObj->{$nodeName}->{"StartString"} = $startString if($startString ne '');
		$jsonObj->{$nodeName}->{"EndString"} = $endString if($endString ne '');
		$solveLine = <FILE>;
		unless($solveLine =~ m/^ searching:\s+([^\s]+)\s+([^\s]+)/){
			# get StartString and EndString...
			next;
		}
		$jsonObj->{$nodeName}->{"StartString"} = $1;
		$jsonObj->{$nodeName}->{"EndString"} = $2;
		while($line = <FILE>){
			if($line =~ m/^ (.+)/){ # normal exit.
				$jsonObj->{$nodeName}->{"Status"} = $1;
				$success = 1 if($1 =~ m/normal/);
			}else{
				$jsonObj->{$nodeName}->{"Status"} = "not solved";
			}
			if($line =~ m/^([^\s]+)\t([^\s]+)\t([^\s]+)/){
				#push(@{$jsonObj->{$nodeName}->{"Result"}->{$2}}, $1);
				$jsonObj->{$nodeName}->{"Result"}->{$2}->{"Answer"} = $1;
				$jsonObj->{$nodeName}->{"Result"}->{$2}->{"Number"} = $3;
			}
		}
		close(FILE);
	}
}
if($success){
	print "success.\n";
}else{
	print "unknown.\n";
}
print to_json($jsonObj);
exit(0);
